package com.derbysoft.lowcostadapter.commons.repository.impl;

import com.derbysoft.common.paginater.Paginater;
import com.derbysoft.lowcostadapter.commons.model.ReservationLog;
import com.derbysoft.lowcostadapter.commons.repository.ReservationLogRepository;
import com.derbysoft.lowcostadapter.commons.utils.DateTimeUtils;
import com.derbysoft.lowcostadapter.web.action.admin.transactionlog.RequestLogCondition;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by: jason
 * Date: 2012-09-18
 */
@Component("reservationLogRepository")
public class ReservationLogRepositoryImpl
        extends CommonService<ReservationLog> implements ReservationLogRepository {

    private static final String OPERATION_TYPE = "operationType";
    private static final String ERSP = "ersp";
    private static final String HOTELS = "hotels";
    private static final String TASK_ID = "taskId";
    private static final String TIMESTAMP = "timestamp";
    private static final String TIME_SPAN = "timeSpan";

    @Override
    public void findByCondition(RequestLogCondition condition, Paginater paginater) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ReservationLog.class).addOrder(Order.desc("id"));
        if (condition.getCode() != null) {
            detachedCriteria.add(Restrictions.eq(OPERATION_TYPE, condition.getCode()));
        }
        if (StringUtils.isNotBlank(condition.getErspNo())) {
            detachedCriteria.add(Restrictions.eq(ERSP, condition.getErspNo()));
        }
        if (StringUtils.isNotBlank(condition.getHotelCode())) {
            detachedCriteria.add(Restrictions.like(HOTELS, condition.getHotelCode(), MatchMode.ANYWHERE));
        }
        if (StringUtils.isNotBlank(condition.getTaskId())) {
            detachedCriteria.add(Restrictions.eq(TASK_ID, condition.getTaskId()));
        }
        if (condition.isHasError()) {
            detachedCriteria.add(Restrictions.eq("error", condition.isHasError()));
        }
        if (StringUtils.isNotBlank(condition.getStart()) && StringUtils.isNotBlank(condition.getEnd())) {
            detachedCriteria.add(Restrictions.ge(TIMESTAMP, DateTimeUtils.parse(condition.getStart())));
            detachedCriteria.add(Restrictions.le(TIMESTAMP, DateTimeUtils.parse(condition.getEnd())));
        } else if (StringUtils.isNotBlank(condition.getStart())) {
            detachedCriteria.add(Restrictions.ge(TIMESTAMP, DateTimeUtils.parse(condition.getStart())));
        } else if (StringUtils.isNotBlank(condition.getEnd())) {
            detachedCriteria.add(Restrictions.le(TIMESTAMP, DateTimeUtils.parse(condition.getEnd())));
        }
        if (condition.getMinSpentTime() != null && condition.getMaxSpentTime() != null) {
            detachedCriteria.add(Restrictions.ge(TIME_SPAN, condition.getMinSpentTime()));
            detachedCriteria.add(Restrictions.le(TIME_SPAN, condition.getMaxSpentTime()));
        } else if (condition.getMinSpentTime() != null) {
            detachedCriteria.add(Restrictions.ge(TIME_SPAN, condition.getMinSpentTime()));
        } else if (condition.getMaxSpentTime() != null) {
            detachedCriteria.add(Restrictions.le(TIME_SPAN, condition.getMaxSpentTime()));
        }
        paginater(detachedCriteria, paginater);
    }


    @Override
    public List<ReservationLog> findByErsp(String erspNo) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ReservationLog.class);
        detachedCriteria.add(Restrictions.eq(ERSP, erspNo));
        return find(detachedCriteria);
    }

    @Override
    public ReservationLog findById(Long id) {
        return load(id);
    }
}
